package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.ExperimentProcessKzyMapper;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.po.ExperimentProcess;
import com.zhijian.medical.entity.po.ExperimentProcessKzy;
import com.zhijian.medical.entity.vo.request.ExperimentProcessKzySearchVo;
import com.zhijian.medical.entity.vo.request.ExperimentProcessResultSearchVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessKzyVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessOfKzyVo;
import com.zhijian.medical.entity.vo.response.ExperimentProcessResultVo;
import com.zhijian.medical.service.*;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 实验流程-核酸扩增一区-扩增仪数据业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-07-08 22:08:35
 */
@Slf4j
@Service
public class ExperimentProcessKzyServiceImpl implements ExperimentProcessKzyService {

    @Autowired
    private ExperimentProcessKzyMapper mapper;

    @Autowired
    private ExperimentProcessService experimentProcessService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private ExperimentProcessResultService experimentProcessResultService;

    @Autowired
    private ExperimentProcessSpecimenService experimentProcessSpecimenService;

    @Override
    public ExperimentProcessKzy selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ExperimentProcessKzyVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByExperimentProcessId(Long experimentProcessId) {
        mapper.deleteByExperimentProcessId(experimentProcessId);
    }

    @Override
    public Long insert(ExperimentProcessKzy experimentProcessKzy) {
        Integer count = this.count(ExperimentProcessKzySearchVo.builder()
                .experimentProcessId(experimentProcessKzy.getExperimentProcessId())
                .deviceId(experimentProcessKzy.getDeviceId())
                .build());
        AssertUtil.check(count > 0, "此实验流程下已经有此扩增仪模块，请勿重复新增");
        Date now = new Date();
        experimentProcessKzy.setCreateTime(now);
        experimentProcessKzy.setUpdateTime(now);
        return mapper.insert(experimentProcessKzy) > 0 ? experimentProcessKzy.getId() : 0L;
    }

    @Override
    public boolean saveOrUpdate(ExperimentProcessKzy experimentProcessKzy) {
        ExperimentProcess experimentProcess = experimentProcessService.selectById(experimentProcessKzy.getExperimentProcessId());
        AssertUtil.notNull(experimentProcess, "此实验流程不存在或者已被删除");
        Device device = deviceService.selectById(experimentProcessKzy.getDeviceId());
        AssertUtil.notNull(device, "此扩增仪不存在或者已被删除");
        Long id = experimentProcessKzy.getId();
        if (Objects.isNull(id)) {
            //新增
            return this.insert(experimentProcessKzy) != 0;
        } else {
            return this.update(experimentProcessKzy);
        }
    }

    @Override
    public boolean update(ExperimentProcessKzy experimentProcessKzy) {
        Date now = new Date();
        experimentProcessKzy.setUpdateTime(now);
        return mapper.updateByPrimaryKey(experimentProcessKzy) != 0;
    }

    @Override
    public Integer count(ExperimentProcessKzySearchVo experimentProcessKzySearchVo) {
        return mapper.count(experimentProcessKzySearchVo);
    }

    @Override
    public List<ExperimentProcessKzyVo> selectList(ExperimentProcessKzySearchVo experimentProcessKzySearchVo) {
        return mapper.selectList(experimentProcessKzySearchVo);
    }

    @Override
    public Page<ExperimentProcessKzyVo> selectPage(ExperimentProcessKzySearchVo experimentProcessKzySearchVo) {
        //先直接查总数
        int count = count(experimentProcessKzySearchVo);
        //分页
        Page<ExperimentProcessKzyVo> page = new Page<>(true, count, experimentProcessKzySearchVo.getPageNum(), experimentProcessKzySearchVo.getPageSize());
        experimentProcessKzySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<ExperimentProcessKzyVo> responseVos = this.selectList(experimentProcessKzySearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public ExperimentProcessKzyVo selectVoByExperimentProcessIdAndDeviceId(Long experimentProcessId, Long deviceId) {
        return mapper.selectVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId);
    }

    @Override
    public List<ExperimentProcessOfKzyVo> selectExperimentProcessOfKzyVoByExperimentProcessId(Long experimentProcessId) {
        List<ExperimentProcessKzyVo> experimentProcessKzyVos = this.selectList(ExperimentProcessKzySearchVo.builder()
                .experimentProcessId(experimentProcessId)
                .build());
        if (CollectionUtil.isEmpty(experimentProcessKzyVos)) {
            return Collections.emptyList();
        }
        return experimentProcessKzyVos.stream().map(experimentProcessKzyVo -> {
            Long deviceId = experimentProcessKzyVo.getDeviceId();
            ExperimentProcessOfKzyVo experimentProcessOfKzyVo = new ExperimentProcessOfKzyVo();
            experimentProcessOfKzyVo.setDeviceId(deviceId);
            Device device = deviceService.selectById(deviceId);
            experimentProcessOfKzyVo.setDeviceName(Objects.nonNull(device) ? device.getName() : "");
            experimentProcessOfKzyVo.setKzyData(experimentProcessKzyVo);
            List<ExperimentProcessResultVo> experimentProcessResultVos = experimentProcessResultService.selectList(ExperimentProcessResultSearchVo.builder()
                    .experimentProcessId(experimentProcessId)
                    .deviceId(deviceId)
                    .build());
            experimentProcessOfKzyVo.setExperimentProcessResultList(experimentProcessResultVos);
            experimentProcessOfKzyVo.setExperimentProcessSpecimenBatchData(experimentProcessSpecimenService
                    .selectExperimentProcessSpecimenBatchVoByExperimentProcessIdAndDeviceId(experimentProcessId, deviceId));
            return experimentProcessOfKzyVo;
        }).collect(Collectors.toList());
    }

}